package greedyAlgorithm;

import org.junit.Test;

/**
 * @author kunkun
 * @className LeetCode_45
 * @Description ...
 * @date 2025/3/18 9:41
 */
public class LeetCode_121 {

    public int maxProfit(int[] prices) {
        //1. 记录最大值，自身及以后的max
        int[] nums = new int[prices.length];
        nums[nums.length-1] = prices[prices.length-1];
        int max = prices[prices.length-1];
        for (int i = prices.length-2; i >= 0 ; i--) {
            if (prices[i]>max){
                max = prices[i];
            }
            nums[i]=max;
        }
        //2. 计算最大差值
        int result = Integer.MIN_VALUE;
        for (int i = 0; i < prices.length; i++) {
            if (nums[i]-prices[i]>result){
                result = nums[i]-prices[i];
            }
        }
        return result;
    }



}
